extends PlayerState


var hit = false
var impulse: = Vector2.ZERO
var deacceleration: = 0.0
#Impulse strength
var att_spd1: = 1.0 * 60.0
var att_spd2: = 1.0 * 60.0
var att_spd3: = 1.5 * 60.0
#stopping speed
var att_dcc1: = 8.0 * 60.0
var att_dcc2: = 6.0 * 60.0
var att_dcc3: = 5.0 * 60.0
var direction_x = 0.0


func unhandled_input(event: InputEvent):
	if event.is_action_pressed("ui_select"):
		hit = true
		direction_x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")


func physics_process(delta):
	velocity_logic(delta)
	player.gravity_logic(delta)
	player.velocity = player.move_and_slide(player.velocity, Vector2.UP)


func velocity_logic(delta):
	player.velocity = player.velocity.move_toward(Vector2.ZERO, deacceleration * delta)


func enter(msg: Dictionary = {}):
	set_impulse(att_spd1, att_dcc1)
	player.speed = player.run_speed
	animation.play(state_machine.STATE.ATTACK + '1')
	animation.connect("animation_finished", self, "state_check")


func exit():
	animation.disconnect("animation_finished", self, "state_check")


func state_check(anim_name):
	if anim_name == 'Attack1' && hit:
		hit = false
		set_impulse(att_spd2, att_dcc2)
		animation.play(state_machine.STATE.ATTACK + '2')
	elif anim_name == 'Attack2' && hit:
		hit = false
		set_impulse(att_spd3, att_dcc3)
		animation.play(state_machine.STATE.ATTACK + '3')
	elif anim_name == 'Attack3' && hit:
		hit = false
		set_impulse(att_spd1, att_dcc1)
		animation.play(state_machine.STATE.ATTACK + '1')
	else:
		state_machine.transition_to(state_machine.STATE.IDLE)


func set_impulse(imp, dcc):
	impulse.x = imp * player.body.scale.x
	deacceleration = dcc

func add_impulse():
	player.direction.x = direction_x
	player.facing_direction()
	player.velocity = impulse
